package cn.bx.bid.ctrl;

import cn.bx.bid.util.LogUtil;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;

public class RoleInterceptor extends HandlerInterceptorAdapter {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        LogUtil.info("被拦截的方法："+handler+"，时间是："+new Date());
        request.setAttribute("starttime",System.nanoTime());
        return super.preHandle(request, response, handler);
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        LogUtil.info("方法结束了");
       long start= (long) request.getAttribute("starttime");

        super.afterCompletion(request, response, handler, ex);
        long time= System.nanoTime()-start;
        LogUtil.info(time);
    }
}
